#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using MobileConfig;

#endregion

namespace MobileConfig
{
	/// <summary>
	/// Summary description for Config.
	/// </summary>
	public class ConfigForm : System.Windows.Forms.Form
	{
		private MenuItem menuItem1;
		private MenuItem menuItem2;		
		
		private Label[] Question;
		private ComboBox[] Values;
		private TextBox[] Value;
		/// <summary>
		/// Main menu for the form.
		/// </summary>
		private System.Windows.Forms.MainMenu mainMenu1;
		private ResizeLabel Description;

		private Config cfg;

		public ConfigForm(Config config)
		{
			cfg = config;			
			InitializeComponent();			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.Description = new ResizeLabel();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.menuItem1);
			this.mainMenu1.MenuItems.Add(this.menuItem2);
			// 
			// menuItem1
			// 
			this.menuItem1.Text = "Apply";
			this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Text = "Cancel";
			this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
			// 
			// Description
			// 
			this.Description.Font = new System.Drawing.Font("Segoe Condensed", 9F, System.Drawing.FontStyle.Regular);
			this.Description.Location = new System.Drawing.Point(3, 3);
			this.Description.Size = new System.Drawing.Size(240, 20);
			this.Description.Text = "MobileConfig";
			// 
			// ConfigForm
			// 
			this.ClientSize = new System.Drawing.Size(176, 180);
			this.Controls.Add(this.Description);
			this.Menu = this.mainMenu1;
			this.Text = "Config";
			this.Load += new System.EventHandler(this.Config_Load);

		}

		#endregion

		private void menuItem2_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void Config_Load(object sender, EventArgs e)
		{			
			this.Text = cfg.Name;
			Description.Text = cfg.Description;
			Description.Width = this.Width - 6;		
			
			if (cfg.Questions != null)
			{
				int lastTop = this.Description.Top + this.Description.Height + 4;
				bool firstQ = true;
				foreach (Question q in cfg.Questions)
				{
					QuestionPanel qp = new QuestionPanel(q);
					this.Controls.Add(qp);
					qp.Location = new Point(3, lastTop);
					qp.Width = this.Width - 6;
					lastTop = qp.Top + qp.Height + 4;
					if (firstQ)
					{
						qp.BringToFront();
						qp.GiveFocus();
					}
					firstQ = false;
				}
			}

		}


		private void menuItem1_Click(object sender, EventArgs e)
		{
			if(cfg != null && cfg.Questions != null)
			{
				foreach (Control c in this.Controls)
				{
					if (c is QuestionPanel)
					{
						Question q = ((QuestionPanel) c).Question;
						
						if (q != null && q.Values != null)
						{
							if (q.Values.Length == 1)
							{
								foreach (RegValue rv in q.Values[0].RegValues)
								{
									if (rv.Value == null)
										rv.Value = ((QuestionPanel)c).SelectedValue.ToString();

									if (rv.type == "0")
									{
										try
										{
											int outVal = Convert.ToInt32(((QuestionPanel)c).SelectedValue);
											rv.Value = outVal.ToString();
										}
										catch
										{
											MessageBox.Show("A numeric value is required!");
											return;
										}
									}

									if (rv.Value != null)
									{
										if (rv.type == "0")
											Utility.SetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, Convert.ToInt32(rv.Value));
										if (rv.type == "1")
											Utility.SetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, rv.Value);
										if (rv.type == "2")
											Utility.SetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, HEXStringToByteArray(rv.Value.Split(',')));
									}
								}
							}

							if (q.Values.Length > 1)
							{
								Value selV = (Value)((QuestionPanel)c).SelectedValue;
								if (selV.RegValues != null)
								{
									foreach (RegValue rv in selV.RegValues)
									{
										if (rv.Value == null && selV.value != null)
											rv.Value = selV.value;

										if (rv.Value != null)
										{
											if (rv.type == "0")
												Utility.SetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, Convert.ToInt32(rv.Value));
											if (rv.type == "1")
												Utility.SetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, rv.Value);
											if (rv.type == "2")
												Utility.SetRegKeyValue(Utility.GetRootKey(rv.Key), rv.SubKey, rv.Name, HEXStringToByteArray(rv.Value.Split(',')));
										}


									}
								}
							}
						}					
					}
				}
			}
			Close();
		}

		private byte[] HEXStringToByteArray(string[] InHex)
		{
			byte[] outHex = new byte[InHex.Length];
			for (int i = 0; i < InHex.Length; i++)
			{
				outHex[i] = byte.Parse(InHex[i], System.Globalization.NumberStyles.HexNumber);				
			}
			return outHex;
		}
	}
}
